<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CityBusyPeriod extends Model
{
    public $timestamps = false;
    protected $table   = 'city_busy_period';

    public function getCityBusyPeriodsWithDefault($cityId)
    {
        $data = self::getCityBusyPeriods($cityId);
        if (!$data) {
            $data = self::getDefaultCityBusyPeriod();
        }

        return $data;
    }

    public function getDefaultCityBusyPeriod()
    {
        $data = static::where(['city_id' => 0])->get()->toArray();

        return $data;
    }

    public function getCityBusyPeriods($cityId)
    {
        $data = static::where(['city_id' => $cityId])->get()->toArray();

        return $data;
    }

    public function clearCityBusyPeriods(int $cityId)
    {
        return static::where(['city_id' => $cityId])->delete();
    }

    public function addBatchCityBusyPeriods(array $entityList)
    {
        $arr = [];
        foreach ((array) $entityList as $entity) {
            $arr[] = get_object_vars($entity);
        }

        return static::insert($arr);
    }
}
